// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0

Item {
    id: addDefect
    anchors.fill: parent

    property double holeLatitude: -1
    property double holeLongitude: -1

    LocationPicker { id: locationPicker }
    PhotoPicker {
        id: photoPicker
        onImageSelected: imagesModel.append({image: image})
    }

    ListModel { id: imagesModel }

    QueryDialog {
            id: deleteImageDialog
            message: qsTr("Delete this image?")
            acceptButtonText: qsTr("Delete")
            rejectButtonText: qsTr("Cancel")
            property int imageIndex: -1

            onAccepted: {
                rosYama.removeFile(imagesModel.get(imageIndex).image)
                imagesModel.remove(imageIndex)
            }
   }

    Component {
        id: photoDelegate
        Image {
            width: 160
            height: 160
            clip: true
            fillMode: Image.PreserveAspectCrop
            source: image
            sourceSize.width: 160
            smooth: true

            MouseArea {
                anchors.fill: parent
                onPressAndHold: {
                    deleteImageDialog.imageIndex = index
                    deleteImageDialog.open()
                }
            }
        }
    }
    Component {
        id: addImageFooter
        Item {
            width: 160
            height: 160
            ListButton {
                width: 100
                height: 100
                anchors.centerIn: parent
                iconSource: "image://theme/icon-m-toolbar-add"
                onClicked: pageStack.push(photoPicker)
                enabled: imagesModel.count < 10
            }
        }
    }

    PageHeader {
        id: header
        text: qsTr("Add defect")
        BusyIndicator {
            id: busyIndicator
            running: false
            visible: false
            anchors {
                verticalCenter: parent.verticalCenter
                right: parent.right
                margins: UiConstants.DefaultMargin
            }
        }
    }

    Flickable {
        id: container
        anchors {
            top: header.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
            margins: UiConstants.DefaultMargin
        }

        contentWidth: col.width
        contentHeight: col.height
        flickableDirection: Flickable.VerticalFlick
        pressDelay: 100

        Column {
            id: col
            spacing: 10
            width: container.width

            Label {
                text: qsTr("Address")
            }
            Row {
                anchors {left: parent.left; right: parent.right;}
                width: parent.width
                spacing: 10
                TextArea {
                    width: parent.width - selectOnMapButton.width - 10
                    id: addressArea
                    placeholderText: qsTr("Type address or select from map below")
                }
                ListButton {
                    id: selectOnMapButton
                    anchors.verticalCenter: parent.verticalCenter
                    iconSource: "image://theme/icon-s-calendar-location-picker"
                    onClicked: locationPicker.open()
                }
            }

            Label {
                text: qsTr("Photos")
            }
            ListView {
                id: imagesList
                model: imagesModel
                clip: true
                width: parent.width
                height: 160
                spacing: 10
                cacheBuffer: 640
                orientation: ListView.Horizontal
                delegate: photoDelegate
                footer: addImageFooter
            }
            Button {
                anchors {left: parent.left; right: parent.right;}
                text: holeType.model.get(holeType.selectedIndex).name
                iconSource: holeType.model.get(holeType.selectedIndex).iconSource
                onClicked: holeType.open();
            }
            TextArea {
                id: commentArea
                anchors {left: parent.left; right: parent.right;}
                placeholderText: qsTr("Your comment")
                height: Math.max (250, implicitHeight)
            }
            Button {
                id: submitButton
                anchors {left: parent.left; right: parent.right;}
                text: qsTr("Submit")
                onClicked: {
                    submitButton.enabled = false
                    busyIndicator.visible = true
                    busyIndicator.running = true

                    var photoFiles=new Array()
                    for (var i = 0; i < imagesModel.count; i++) {
                        photoFiles.push(imagesModel.get(i).image)
                    }

                    rosYama.submitHole(addressArea.text, Qt.point(holeLatitude,holeLongitude),
                                       commentArea.text, holeType.model.get(holeType.selectedIndex).code,
                                       photoFiles)
                }
            }
        }
    }

    SelectionDialog {
        id: holeType
        titleText: qsTr("Select defect type")

        model: ListModel {}

        Component.onCompleted: {
            model.append({ name: qsTr("Bad road"),     code: "badroad",      iconSource: "../../images/badroad.png" })
            model.append({ name: qsTr("Hole on road"), code: "holeonroad",   iconSource: "../../images/holeonroad.png" })
            model.append({ name: qsTr("Hatch"),        code: "hatch",        iconSource: "../../images/hatch.png" })
            model.append({ name: qsTr("Rails"),        code: "rails",        iconSource: "../../images/rails.png" })
            model.append({ name: qsTr("Hole in yard"), code: "holeinyard",   iconSource: "../../images/holeinyard.png" })
            model.append({ name: qsTr("Snow"),         code: "snow",         iconSource: "../../images/snow.png" })

            selectedIndex = 0
        }
    }

    Connections {
        target: rosYama
        onSubmitHoleResult: {
            if (submitOk) {
                tabGroup.currentTab = listDefects
                imagesModel.clear()
                addressArea.text = ""
                commentArea.text = ""
                holeType.selectedIndex = 0
                listDefects.reloadData()
            }
            submitButton.enabled = true
            busyIndicator.visible = false
            busyIndicator.running = false
        }
    }
}
